/*
 * File: OpeningEditorTopComponent.java
 * Date     Author      Changes
 * 2/13/13  rejun001    original version
 */
package idea.iquest.editor;


import idea.apptopcomponents.AppTopComponents;
import idea.iquest.explorer.IQuestionnairesTopComponent;
import idea.model.dbmanager.QueryManager;

import idea.model.powersearch.CCISearchResultsTopComponent;
import idea.model.powersearch.NoiseWords;
import idea.model.powersearch.QTSearchResultsTopComponent;
import idea.model.qnrlibrary.CustomContentItemSearchView;
import idea.model.qnrlibrary.QuestionTitleSearchView;
import idea.model.qnrlibrary.Questionnaires;
import idea.model.qnrlibrary.ReportingPeriods;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.DialogDisplayer;
import org.openide.LifecycleManager;
import org.openide.NotifyDescriptor;
import org.openide.NotifyDescriptor.Message;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.StatusDisplayer;
import org.openide.awt.UndoRedo;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.InstanceContent;
import org.openide.windows.WindowManager;


/**
 * class OpeningEditorTopComponent -- a main window which Displays the six
 * criteria to search on to bring up a table of Questionnaires which match the
 * criteria. Also displays the Power Search panel to search for a phrase through
 * several tables.
 *
 * @see TopComponent
 * @see LookupListener
 * @version 1.1 March 2013
 * @author rejun001
 *
 *
 */
@ConvertAsProperties(
        dtd = "-//idea.iquest.editor//OpeningEditor//EN" ,
        autostore = false )
@TopComponent.Description(
        preferredID = "OpeningEditorTopComponent" ,
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( mode = "explorer" , openAtStartup = true )
@ActionID( category = "Window" , id = "idea.iquest.editor.OpeningEditorTopComponent" )
@ActionReferences(
        {
    @ActionReference( path = "Menu/Window" , position = 333 ) ,
    @ActionReference( path = "Menu/File/Open" , position = 1200 )
} )
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_OpeningEditorAction" ,
        preferredID = "OpeningEditorTopComponent" )
@Messages(
        {
    "CTL_OpeningEditorAction=Opening Editor" ,
    "CTL_OpeningEditorTopComponent=Opening Editor Window" ,
    "HINT_OpeningEditorTopComponent=This is the Opening Editor window"
} )
public final class OpeningEditorTopComponent extends TopComponent implements LookupListener
{

    /**
     * Placeholder for result of lookup of QuestionnairesTopComponent
     */
    private Lookup.Result result = null;
    /**
     * Manager for undo and redo
     */
    private UndoRedo.Manager urMgr = new UndoRedo.Manager();
    /**
     * not used -- lookup stuff
     */
    private InstanceContent instanceContent = new InstanceContent();
    private Questionnaires questionnaires;
    /**
     * instance of a class which contains refs to all other TopComponents
     */
    private AppTopComponents appTCs;


    /**
     * Constructor Inits swing components Inits combo boxes and text fields for
     * ctriteria Sets up printing and undo-redo Sets up status bar (not working)
     */
    public OpeningEditorTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_OpeningEditorTopComponent() );
        setToolTipText( Bundle.HINT_OpeningEditorTopComponent() );
        
        initComboBoxes();
        appTCs = new AppTopComponents();
        
        putClientProperty( "print.printable" , Boolean.TRUE );
        putClientProperty( "print.name" , "Ed's Questionnaire Criteria" );
        
        txtTitle.getDocument().addUndoableEditListener( urMgr );
        txtSubTitle.getDocument().addUndoableEditListener( urMgr );
        txtCreatedBy.getDocument().addUndoableEditListener( urMgr );
        
        txtPowerText.getDocument().addUndoableEditListener( urMgr );
        
        StatusDisplayer.getDefault().setStatusText( "This is Ed's status!!!" );
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnlMain = new javax.swing.JPanel();
        lblMainTitle = new javax.swing.JLabel();
        lblMainTitle1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cbSurvey = new javax.swing.JComboBox();
        lblSurvey = new javax.swing.JLabel();
        lblRefPeriod = new javax.swing.JLabel();
        cbRefPeriod = new javax.swing.JComboBox();
        lblFormNumber = new javax.swing.JLabel();
        cbFormNumber = new javax.swing.JComboBox();
        lblCreatedBy = new javax.swing.JLabel();
        txtCreatedBy = new javax.swing.JTextField();
        txtSubTitle = new javax.swing.JTextField();
        lblSubTitle = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        lblTitle = new javax.swing.JLabel();
        lblQuestionnaires = new java.awt.Label();
        btnSearch = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        pnlPowerSearch = new javax.swing.JPanel();
        lblPowerSearch = new javax.swing.JLabel();
        lblSurvey1 = new javax.swing.JLabel();
        cbSurvey1 = new javax.swing.JComboBox();
        lblRefPeriod1 = new javax.swing.JLabel();
        cbRefPeriod1 = new javax.swing.JComboBox();
        txtPowerText = new javax.swing.JTextField();
        lblPowerText = new javax.swing.JLabel();
        btnPowerSearch = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        txtCat = new javax.swing.JTextField();
        lblCat = new javax.swing.JLabel();
        lblQTVersion = new java.awt.Label();
        cbVersion = new javax.swing.JComboBox();
        lblOpt = new java.awt.Label();
        jPanel1 = new javax.swing.JPanel();
        btnClear = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.jLabel2.text")); // NOI18N

        pnlMain.setBackground(new java.awt.Color(153, 204, 255));

        lblMainTitle.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblMainTitle, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblMainTitle.text")); // NOI18N

        lblMainTitle1.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblMainTitle1, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblMainTitle1.text")); // NOI18N

        jPanel2.setBackground(new java.awt.Color(153, 204, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        cbSurvey.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no survey]", "Survey 1", "Survey 2", "Survey 3" }));
        cbSurvey.addItemListener(new java.awt.event.ItemListener()
        {
            public void itemStateChanged(java.awt.event.ItemEvent evt)
            {
                cbSurveyItemStateChanged(evt);
            }
        });

        lblSurvey.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblSurvey, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblSurvey.text")); // NOI18N

        lblRefPeriod.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblRefPeriod, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblRefPeriod.text")); // NOI18N

        cbRefPeriod.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no ref period]", "Ref Period 1", "Ref Period 2", "Ref Period 3", "Ref Period 4" }));
        cbRefPeriod.addItemListener(new java.awt.event.ItemListener()
        {
            public void itemStateChanged(java.awt.event.ItemEvent evt)
            {
                cbRefPeriodItemStateChanged(evt);
            }
        });

        lblFormNumber.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblFormNumber, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblFormNumber.text")); // NOI18N

        cbFormNumber.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no form number]", "Form Number 1", "Form Number 2", "Form Number 3", "Form Number 4" }));

        lblCreatedBy.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblCreatedBy, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblCreatedBy.text")); // NOI18N

        txtCreatedBy.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtCreatedBy.text")); // NOI18N
        txtCreatedBy.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                txtCreatedByActionPerformed(evt);
            }
        });

        txtSubTitle.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtSubTitle.text")); // NOI18N

        lblSubTitle.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblSubTitle, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblSubTitle.text")); // NOI18N

        txtTitle.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtTitle.text")); // NOI18N

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblTitle, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblTitle.text")); // NOI18N

        lblQuestionnaires.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblQuestionnaires.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblQuestionnaires.text")); // NOI18N

        btnSearch.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(btnSearch, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.btnSearch.text")); // NOI18N
        btnSearch.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(102, 102, 102)
                                .addComponent(lblQuestionnaires, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblRefPeriod)
                                    .addComponent(cbRefPeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cbSurvey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblSurvey))
                                .addGap(66, 66, 66)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblCreatedBy, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblSubTitle)
                                        .addComponent(txtSubTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                                        .addComponent(txtTitle))
                                    .addComponent(txtCreatedBy, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(54, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFormNumber)
                            .addComponent(cbFormNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(btnSearch)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblQuestionnaires, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSurvey)
                    .addComponent(lblTitle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbSurvey, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblSubTitle)
                    .addComponent(lblRefPeriod))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSubTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbRefPeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFormNumber)
                    .addComponent(lblCreatedBy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbFormNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCreatedBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnSearch)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.jLabel1.text")); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        pnlPowerSearch.setBackground(new java.awt.Color(153, 204, 255));
        pnlPowerSearch.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblPowerSearch.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblPowerSearch, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblPowerSearch.text")); // NOI18N

        lblSurvey1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblSurvey1, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblSurvey1.text")); // NOI18N

        cbSurvey1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no survey]", "Survey 1", "Survey 2", "Survey 3" }));
        cbSurvey1.addItemListener(new java.awt.event.ItemListener()
        {
            public void itemStateChanged(java.awt.event.ItemEvent evt)
            {
                cbSurvey1ItemStateChanged(evt);
            }
        });

        lblRefPeriod1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblRefPeriod1, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblRefPeriod1.text")); // NOI18N

        cbRefPeriod1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no ref period]", "Ref Period 1", "Ref Period 2", "Ref Period 3", "Ref Period 4" }));
        cbRefPeriod1.addItemListener(new java.awt.event.ItemListener()
        {
            public void itemStateChanged(java.awt.event.ItemEvent evt)
            {
                cbRefPeriod1ItemStateChanged(evt);
            }
        });

        txtPowerText.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtPowerText.text")); // NOI18N
        txtPowerText.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                txtPowerTextActionPerformed(evt);
            }
        });

        lblPowerText.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblPowerText, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblPowerText.text")); // NOI18N

        btnPowerSearch.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(btnPowerSearch, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.btnPowerSearch.text")); // NOI18N
        btnPowerSearch.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnPowerSearchActionPerformed(evt);
            }
        });

        jPanel3.setBackground(new java.awt.Color(135, 179, 223));
        jPanel3.setBorder(new javax.swing.border.MatteBorder(null));

        txtCat.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtCat.text")); // NOI18N

        lblCat.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblCat, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblCat.text")); // NOI18N

        lblQTVersion.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        lblQTVersion.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblQTVersion.text")); // NOI18N

        cbVersion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbVersion.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                cbVersionActionPerformed(evt);
            }
        });

        lblOpt.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        lblOpt.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblOpt.text")); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(lblOpt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCat)
                            .addComponent(lblQTVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCat, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblOpt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblQTVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCat)
                    .addComponent(txtCat, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnlPowerSearchLayout = new javax.swing.GroupLayout(pnlPowerSearch);
        pnlPowerSearch.setLayout(pnlPowerSearchLayout);
        pnlPowerSearchLayout.setHorizontalGroup(
            pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPowerSearchLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbSurvey1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSurvey1)
                    .addComponent(lblRefPeriod1)
                    .addComponent(cbRefPeriod1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPowerSearchLayout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(btnPowerSearch))
                    .addGroup(pnlPowerSearchLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPowerText)
                            .addComponent(txtPowerText, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPowerSearch)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        pnlPowerSearchLayout.setVerticalGroup(
            pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPowerSearchLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(lblPowerSearch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlPowerSearchLayout.createSequentialGroup()
                        .addGroup(pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblSurvey1)
                            .addComponent(lblPowerText))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlPowerSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbSurvey1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPowerText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(35, 35, 35)
                        .addComponent(lblRefPeriod1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbRefPeriod1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(74, 74, 74))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnPowerSearch)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jPanel1.setBackground(new java.awt.Color(153, 204, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        btnClear.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(btnClear, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.btnClear.text")); // NOI18N
        btnClear.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnClearActionPerformed(evt);
            }
        });

        btnExit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(btnExit, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.btnExit.text")); // NOI18N
        btnExit.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(btnClear)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnExit)
                .addGap(30, 30, 30))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClear)
                    .addComponent(btnExit))
                .addGap(18, 18, 18))
        );

        javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
        pnlMain.setLayout(pnlMainLayout);
        pnlMainLayout.setHorizontalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pnlPowerSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(lblMainTitle1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(lblMainTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(338, Short.MAX_VALUE))
        );
        pnlMainLayout.setVerticalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMainTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMainTitle1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(pnlPowerSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(118, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(pnlMain);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 936, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37))
        );
    }// </editor-fold>//GEN-END:initComponents


    /**
     * Search button action. Searches for Questionnaires that match the chosen
     * criteria
     *
     * @param evt-- ActionEvent
     */
    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        appTCs.closeCrudTCs();
        appTCs.closeDesignerTCs();
        appTCs.closeSearchTCs();
        appTCs.closeSfoTCs();

//        TopComponent treeTC = appTCs.getTreeTC();
//        treeTC.close();

        String refPeriod = null;
        Object item = cbRefPeriod.getSelectedItem();
        /**
         * JPA has a quirk that, whereas the RefPeriod in the questionnaires
         * table is a GUID (string), it sometimes reads it as the
         * ReportingPeriods object the GUID points to.
         */
        if ( item instanceof ReportingPeriods )
        {
            ReportingPeriods rp = (ReportingPeriods) item;
            refPeriod = rp.getRefPeriod();
        }
        else if ( item instanceof String )
        {
            refPeriod = (String) item;
            
        }
        // create and run the query based on the six criteria
        List<Questionnaires> resultList = QueryManager.buildQnrQuery(
                (String) cbSurvey.getSelectedItem() ,
                txtTitle.getText() ,
                txtSubTitle.getText() ,
                (String) cbFormNumber.getSelectedItem() ,
                refPeriod ,
                txtCreatedBy.getText() );

        // Go get the Questionnaires top component and display the result set.

        IQuestionnairesTopComponent iqtc = appTCs.getQnrTC();
        iqtc.displayQuestionnaires( resultList );
        
        openTC( iqtc );
        
    }//GEN-LAST:event_btnSearchActionPerformed


    /**
     * Clear button action
     *
     * @param evt -- ActionEvent
     */
    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        clearTextFields();
        initComboBoxes();
        
        appTCs.closeCrudTCs();
        appTCs.closeSfoTCs();
        appTCs.closeDesignerTCs();
        appTCs.closeOneOffTcs();
        appTCs.closeSearchTCs();
        
    }//GEN-LAST:event_btnClearActionPerformed


    /**
     * Clear the three text fields, plus the power text field
     */
    private void clearTextFields()
    {
        txtTitle.setText( "" );
        txtSubTitle.setText( "" );
        txtCreatedBy.setText( "" );
        
        txtPowerText.setText( "" );
    }


    /**
     * Exit the application.
     *
     * @param evt
     */
    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        LifecycleManager.getDefault().exit();
    }//GEN-LAST:event_btnExitActionPerformed


    /**
     * If a different survey is picked in the combo box, populate the other two
     * combo boxes correctly
     *
     * @param evt -- ItemEvent
     */
    private void cbSurveyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSurveyItemStateChanged
        
        populateRefPeriodComboBox( (String) cbSurvey.getSelectedItem() );
        
        Object item = cbRefPeriod.getSelectedItem();   // that pesky ReportingPeriods vs. String object again
        populateFormNumComboBox( (String) cbSurvey.getSelectedItem() , item );
        
        clearTextFields();
    }


    /**
     * populate the ref period combo box with the appropriate ref periods for
     * that survey.
     *
     * @param survey -- String
     */
    private void populateRefPeriodComboBox( String survey )
    {
        List<String> refPds = QueryManager.getRefPeriods( survey );

        // add an empty ref period so user can search on all
        refPds.add( refPds.size() , "" );
        //refPds.add( 0 , "" );
        cbRefPeriod.setModel( new DefaultComboBoxModel( refPds.toArray() ) );
        //cbRefPeriod1.setModel( new DefaultComboBoxModel( refPds.toArray() ) );
    }


    /**
     * populate the ref period combo box FOR POWER SEARCH
     *
     * @param survey
     */
    private void populateRefPeriod1ComboBox( String survey )
    {
        List<String> refPds = QueryManager.getRefPeriods( survey );

        // add an empty ref period so user can search on all
        refPds.add( refPds.size() , "" );
        //refPds.add( 0 , "" );
        //cbRefPeriod.setModel( new DefaultComboBoxModel( refPds.toArray() ) );
        cbRefPeriod1.setModel( new DefaultComboBoxModel( refPds.toArray() ) );
    }
    

    /**
     * 
     * @param survey
     * @param refPeriod 
     */
    private void populateVersionComboBox( String survey , String refPeriod )
    {
        int maxVersion = QueryManager.getMaxVersion( survey , refPeriod );
        String[] versions = new String[ maxVersion + 1 ];
        for ( int i = 0; i < maxVersion + 1; i++ )
        {
            if ( i == 0 )
            {
                versions[0] = "";
            }
            else
            {
                versions[i] = "" + i;
            }            
        }
        cbVersion.setModel( new DefaultComboBoxModel( versions ) );
    }


    /**
     * populate the form number combo box with the appropriate ref periods for
     * that survey / ref period.
     *
     * @param survey -- String
     * @param refPd -- note it is an Object, could be either String or
     * ReportingPeriods
     */
    public void populateFormNumComboBox( String survey , Object refPd )
    {
        List<String> formNums = new ArrayList<>();
        if ( refPd instanceof String )
        {
            formNums = QueryManager.getFormNumbers( survey , (String) refPd );
        }
        else if ( refPd instanceof ReportingPeriods )
        {
            formNums = QueryManager.getFormNumbers( survey , (ReportingPeriods) refPd );
        }
        // add an empty form num so user can search on all
        //formNums.add( 0 , "" );
        formNums.add( formNums.size() , "" );
        cbFormNumber.setModel( new DefaultComboBoxModel( formNums.toArray() ) );
    }//GEN-LAST:event_cbSurveyItemStateChanged


    /**
     * When the Ref Period combo box is changed, populate the Form Number cb
     * correctly.
     *
     * @param evt -- ItemEvent
     */
    private void cbRefPeriodItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_cbRefPeriodItemStateChanged
    {//GEN-HEADEREND:event_cbRefPeriodItemStateChanged
        populateFormNumComboBox( (String) cbSurvey.getSelectedItem() , cbRefPeriod.getSelectedItem() ); // second arg is an Object
    }//GEN-LAST:event_cbRefPeriodItemStateChanged
    
    private void txtCreatedByActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_txtCreatedByActionPerformed
    {//GEN-HEADEREND:event_txtCreatedByActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCreatedByActionPerformed


    /**
     * If you change a survey in power search, display the proper ref periods in
     * the combo box
     *
     * @param evt
     */
    private void cbSurvey1ItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_cbSurvey1ItemStateChanged
    {//GEN-HEADEREND:event_cbSurvey1ItemStateChanged
        populateRefPeriod1ComboBox( (String) cbSurvey1.getSelectedItem() );

        //Object item = cbRefPeriod1.getSelectedItem();   // that pesky ReportingPeriods vs. String object again
        //populateFormNumComboBox( (String) cbSurvey.getSelectedItem() , item );
    }//GEN-LAST:event_cbSurvey1ItemStateChanged
    
    private void cbRefPeriod1ItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_cbRefPeriod1ItemStateChanged
    {//GEN-HEADEREND:event_cbRefPeriod1ItemStateChanged
        populateVersionComboBox((String) cbSurvey1.getSelectedItem(), (String) cbRefPeriod1.getSelectedItem());
    }//GEN-LAST:event_cbRefPeriod1ItemStateChanged
    
    private void txtPowerTextActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_txtPowerTextActionPerformed
    {//GEN-HEADEREND:event_txtPowerTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPowerTextActionPerformed


    /**
     * The type of TopComponent to display power search results
     */
    private enum ResultType
    {
        
        QNR, QT, CCI, NONE
    }


    /**
     * ActionPerformed for power search button
     *
     * @param evt
     */
    private void btnPowerSearchActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPowerSearchActionPerformed
    {//GEN-HEADEREND:event_btnPowerSearchActionPerformed
        IQuestionnairesTopComponent qnrtc = appTCs.getQnrTC();
        if (null != qnrtc && null != qnrtc.getOv()) {
            qnrtc.remove( qnrtc.getOv());
            qnrtc.close();
        }
        appTCs.closeSearchTCs();
               
        String survey = (String) cbSurvey1.getSelectedItem();
        String textToSearch = txtPowerText.getText();
        String questionVersion = (String) cbVersion.getSelectedItem();
        String questionCategory = (String) txtCat.getText();
        
        /*
         * JPA has a quirk that, whereas the RefPeriod in the questionnaires
         * table is a GUID (string), it sometimes reads it as the
         * ReportingPeriods object the GUID points to.
         */
        String refPeriod = null;
        Object item = cbRefPeriod1.getSelectedItem();
        if ( item instanceof ReportingPeriods )
        {
            ReportingPeriods rp = (ReportingPeriods) item;
            refPeriod = rp.getRefPeriod();
        }
        else if ( item instanceof String )
        {
            refPeriod = (String) item;
            
        }

        NoiseWords nw = new NoiseWords();
        if ( nw.isNoise( textToSearch ) )
        {
            Message message = new NotifyDescriptor.Message( "Invalid Search Term, please try again." , NotifyDescriptor.ERROR_MESSAGE );
            Object o = DialogDisplayer.getDefault().notify( message );
            
            return;
        }
        if ( textToSearch.isEmpty() )
        {
            Message message = new NotifyDescriptor.Message( "You must enter a Search Term." , NotifyDescriptor.ERROR_MESSAGE );
            Object o = DialogDisplayer.getDefault().notify( message );
            
            return;
        }
        
        ResultType resultType = ResultType.NONE;

        // first query the Questionnaires alone for the text string
        List<Questionnaires> qnrResultList = QueryManager.getQuestionnairesSearchResults( survey , refPeriod , textToSearch );
        
        if ( !qnrResultList.isEmpty() )
        {
            resultType = ResultType.QNR;
        }

        // next query the Question Titles (plus Questionnaires) 
        List<QuestionTitleSearchView> qtResultList = QueryManager.getQuestionTitlesSearchResults( survey , refPeriod , textToSearch, questionVersion, questionCategory );
        
        if ( qtResultList.size() > qnrResultList.size() )
        {
            resultType = ResultType.QT;
        }

        // next query the Custom Content Items (plus Question Titles plus Questionnaires )
        List<CustomContentItemSearchView> cciResultList = QueryManager.getCustomContentItemsSearchResults( survey , refPeriod , textToSearch, questionVersion, questionCategory );
        
        if ( cciResultList.size() > qtResultList.size() )
        {
            resultType = ResultType.CCI;
        }
        
        switch ( resultType )
        {
            
            case QNR:
                //IQuestionnairesTopComponent qnrtc = appTCs.getQnrTC();
                qnrtc.displayQuestionnaires( qnrResultList );
                openTC( qnrtc );
                break;
            case QT:
                QTSearchResultsTopComponent qttc = appTCs.getQtsrTC();
                qttc.displayQTSearchResults( qtResultList );
                openTC( qttc );
                break;
            case CCI:
                CCISearchResultsTopComponent ccitc = appTCs.getCcisrTC();
                //cciResultList = boldify( cciResultList, textToSearch);
                ccitc.displayCCISearchResults( cciResultList );
                openTC( ccitc );
                break;
            case NONE:
            default:
                displayNoSearchResults( false );
                break;
        }
    }//GEN-LAST:event_btnPowerSearchActionPerformed
    
    private void cbVersionActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cbVersionActionPerformed
    {//GEN-HEADEREND:event_cbVersionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbVersionActionPerformed

//    private List<CustomContentItemSearchView> boldify(List<CustomContentItemSearchView> list, String txtToBold) {
//        List<CustomContentItemSearchView> newList = new ArrayList<>();
//        Iterator<CustomContentItemSearchView> it = list.iterator();
//        while (it.hasNext()) {
//            CustomContentItemSearchView cciv = it.next();
//            if (cciv.getCategory().toUpperCase().contains( txtToBold.toUpperCase() )) {
//                String nue = "<html>" + cciv.getCategory() + "</html>";
//                nue = nue.replace( txtToBold, "<b>" + txtToBold + "</b>");
//                cciv.setCategory( nue );
//                
//            }
//            newList.add( cciv );
//        }
//        
//        return newList;
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnPowerSearch;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbFormNumber;
    private javax.swing.JComboBox cbRefPeriod;
    private javax.swing.JComboBox cbRefPeriod1;
    private javax.swing.JComboBox cbSurvey;
    private javax.swing.JComboBox cbSurvey1;
    private javax.swing.JComboBox cbVersion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCat;
    private javax.swing.JLabel lblCreatedBy;
    private javax.swing.JLabel lblFormNumber;
    private javax.swing.JLabel lblMainTitle;
    private javax.swing.JLabel lblMainTitle1;
    private java.awt.Label lblOpt;
    private javax.swing.JLabel lblPowerSearch;
    private javax.swing.JLabel lblPowerText;
    private java.awt.Label lblQTVersion;
    private java.awt.Label lblQuestionnaires;
    private javax.swing.JLabel lblRefPeriod;
    private javax.swing.JLabel lblRefPeriod1;
    private javax.swing.JLabel lblSubTitle;
    private javax.swing.JLabel lblSurvey;
    private javax.swing.JLabel lblSurvey1;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JPanel pnlMain;
    private javax.swing.JPanel pnlPowerSearch;
    private javax.swing.JTextField txtCat;
    private javax.swing.JTextField txtCreatedBy;
    private javax.swing.JTextField txtPowerText;
    private javax.swing.JTextField txtSubTitle;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables


    /**
     * When this window is opened, go get the Questionnaires top component and
     * set up a lookup listener in case this is changed.
     */
    @Override
    public void componentOpened()
    {
        result = WindowManager.getDefault().findTopComponent( "IQuestionnairesTopComponent" )
                .getLookup().lookupResult( Questionnaires.class );
        result.addLookupListener( this );
        resultChanged( new LookupEvent( result ) );
    }


    /**
     * When this is closed, remove the lookup listener.
     */
    @Override
    public void componentClosed()
    {
        
        result.removeLookupListener( this );
        result = null;
    }
    
    
    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }
    
    
    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    /**
     * Set up the combo boxes with all surveys and the Ref periods and form
     * numbers for the first survey.
     */
    void initComboBoxes()
    {
        
        List<String> surveys = QueryManager.getAllSurveys();
        cbSurvey.setModel( new DefaultComboBoxModel( surveys.toArray() ) );
        cbSurvey1.setModel( new DefaultComboBoxModel( surveys.toArray() ) );
        String topSurvey = surveys.get( 0 );

        // init Ref Periods
        List<String> repPeriods = QueryManager.getRefPeriods( topSurvey );

        // here's the pesky ReportingPeriods vs String thing again
        List<ReportingPeriods> rps = new ArrayList<>();
        List<String> refPeriods = new ArrayList<>();
        boolean useRPs = false;
        for ( Object o : repPeriods )
        {
            if ( o instanceof ReportingPeriods )
            {
                useRPs = true;
                ReportingPeriods rp = (ReportingPeriods) o;
                rps.add( rp );
            }
            else if ( o instanceof String )
            {
                refPeriods.add( (String) o );
            }
        }

        String topRefPeriod = refPeriods.get( 0);
        // init Form Numbers
        List<String> formNumbers;
        if ( useRPs )
        {
            ReportingPeriods emptyRP = new ReportingPeriods();
            emptyRP.setRefPeriod( "" );
            rps.add( rps.size() , emptyRP );
            
            cbRefPeriod.setModel( new DefaultComboBoxModel( rps.toArray() ) );
            cbRefPeriod1.setModel( new DefaultComboBoxModel( rps.toArray() ) );
            
            ReportingPeriods topRP = rps.get( 0 );
            formNumbers = QueryManager.getFormNumbers( topSurvey , topRP ); // this has two signatures for String & RP
        }
        else // Strings
        {
            cbRefPeriod.setModel( new DefaultComboBoxModel( refPeriods.toArray() ) );
            cbRefPeriod1.setModel( new DefaultComboBoxModel( refPeriods.toArray() ) );
            
            topRefPeriod = refPeriods.get( 0 );
            formNumbers = QueryManager.getFormNumbers( topSurvey , topRefPeriod ); // this has two signatures for String & RP
        }
        
        formNumbers.add( formNumbers.size() , "" ); // an empty one to search for all Form Numbers
        cbFormNumber.setModel( new DefaultComboBoxModel( formNumbers.toArray() ) );
        
        populateVersionComboBox(topSurvey, topRefPeriod);
    }


    /**
     * If the Opening Top Component changes
     *
     * @param le -- LookupEvent
     */
    @Override
    public void resultChanged( LookupEvent le )
    {
        Lookup.Result r = (Lookup.Result) le.getSource();
        Collection<Questionnaires> coll = (Collection<Questionnaires>) r.allInstances();
        if ( !coll.isEmpty() )
        {
            for ( Questionnaires q : coll )
            {
                questionnaires = q;                    // the default questionnaire
                txtTitle.setText( q.getTitle() );
                txtSubTitle.setText( q.getSubTitle() );
                txtCreatedBy.setText( q.getCreatedBy() );
                cbSurvey.setSelectedItem( q.getSurvey() );
                cbRefPeriod.setSelectedItem( q.getRefPeriod() );
                cbFormNumber.setSelectedItem( q.getFormNumber() );
                
                cbSurvey1.setSelectedItem( q.getSurvey() );
                cbRefPeriod1.setSelectedItem( q.getRefPeriod() );
            }
        }
        else
        {
            // ???
        }
    }


    /**
     * Display an empty Questionnaires window with NO Search Results Found
     * @param invalid -- not used: Invalid search term is used in idea.model.powersearch.NoiseWords class 
     */
    void displayNoSearchResults( boolean invalid )
    {
        CCISearchResultsTopComponent ccitc = appTCs.getCcisrTC();
        //IQuestionnairesTopComponent qnrtc = appTCs.getQnrTC();
        ccitc.close();
        String msg = "No Search Results Found";
        if ( invalid )
        {
            msg = "Invalid Search Term";
        }
        ccitc.getLblCCISearchResults().setText( msg );
        JLabel blankLabel = new JLabel( "-*-" );
        
        ccitc.add( ccitc.getLblCCISearchResults() , BorderLayout.NORTH );
        ccitc.add( blankLabel , BorderLayout.CENTER );
        openTC( ccitc );
    }


    /**
     * Getter for undo redo manager
     *
     * @return
     */
    @Override
    public UndoRedo getUndoRedo()
    {
        return urMgr;
    }


    /**
     * open a top component window
     *
     * @param tc
     */
    private void openTC( TopComponent tc )
    {
        tc.open();
        tc.requestActive();
    }
    
}
